package com.layui.ad.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.layui.ad.annotation.DataSource;
import com.layui.ad.entity.RoleMenu;
import com.layui.ad.entity.SysMenu;
import com.layui.ad.enums.DataSourceEnum;
import com.layui.ad.mapper.RoleMenuMapper;
import com.layui.ad.mapper.SysMenuMapper;
import com.layui.ad.service.RoleMenuService;
import com.layui.ad.utils.JsonResult;
import com.layui.ad.utils.StringUtils;
import com.layui.ad.vo.RolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

        @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;


    /**
     * 根据角色ID获取菜单列表
     *
     * @param roleId 角色ID
     * @return
     */
    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult getRolePermissionByRoleId(Integer roleId){
        // 查询条
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("sort");
        List<SysMenu> menuList = sysMenuMapper.selectList(queryWrapper);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (!menuList.isEmpty()) {
            // 获取已配置菜单权限
            List<RoleMenu> roleMenuList = this.getRoleMenuListByRoleId(roleId);
            List<Integer> integerList = roleMenuList.stream().map(o -> o.getMenuId()).collect(Collectors.toList());
            menuList.forEach(item -> {
                Map<String, Object> map = new HashMap<>();
                // 菜单ID
                map.put("id", item.getId());
                // 菜单名称
                map.put("name", item.getName());
                // 上级ID
                map.put("pId", item.getPid());
                // 是否选中
                if (integerList.contains(item.getId())) {
                    map.put("checked", true);
                } else {
                    map.put("checked", false);
                }
                // 是否展开
                map.put("open", true);
                mapList.add(map);
            });
        }
        return JsonResult.ok(mapList);
    }
    /**
     * 根据ID获取角色菜单列表
     *
     * @param roleId 角色ID
     * @return
     */
    private List<RoleMenu> getRoleMenuListByRoleId(Integer roleId) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        List<RoleMenu> roleMenuList = roleMenuMapper.selectList(queryWrapper);
        return roleMenuList;
    }


    /**
     * 设置权限
     */
    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public JsonResult setRolePermission(RolePermissionVo rolePermissionVo){
        if(null == rolePermissionVo.getRoleId()|| rolePermissionVo.getRoleId()<0){
            return JsonResult.fail("角色ID不能为空");
        }
        if(StringUtils.isEmpty(rolePermissionVo.getAuthIds())){
            return JsonResult.fail("请选择菜单权限");
        }
        String[] authList = rolePermissionVo.getAuthIds().split(",");
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",rolePermissionVo.getRoleId());
        roleMenuMapper.delete(queryWrapper);

        for(String s:authList){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(Integer.valueOf(s));
            roleMenu.setRoleId(rolePermissionVo.getRoleId());
            roleMenuMapper.insert(roleMenu);
        }
        return JsonResult.ok();
    }
}
